package com.infinitus.autocatlog.domain;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;



import com.infinitus.autocatlog.service.IpService;
import com.infinitus.autocatlog.service.util.Base64Coder;
import com.infinitus.autocatlog.service.util.TimeUtil;


@Entity
@Table(name="event_item")
public class EventItem implements Serializable{
	public static final String ITEM_TERMINATED = "\001";

	public static final String COLLECTION_TERMINATED = "\002";
	
	public static final String HEAD_TERMINATED = "\\|";

	public static final String MAP_TERMINATED = "\003";
	/**
	 * 
	 */
	private static final long serialVersionUID = 2896410356561899013L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long id;
	private String event_screen;
	private String event_action;
	private String event_reason;
	private String event_label;
	private String event_value;
	private String event_extra;
	private String user_id;
	private String session_id;
	private String EventDateTime;
	private String ip;
	private String nation;
	private String province;
	private String city;
	private String operator;
	private String network;
	private String system_name;
	private String system_version;
	private String client_device_id;
	private String client_device_brand;
	private String client_device_model;
	private String client_device_agent;
	private String client_device_version;
	private String os_screen;
	private String referrer;
	private String campaign;
	private String source;
	private String media;
	private String creative;
	private String remark;

	private String EventDate;
	
	private String EventHour;
	
//	private String Content;
	
	private String UserID4Num;
	
//	private String OptName;

	@Column(name="the_month")
	private String YearMonth;

	@Column(name="the_week")
	private String YearWeek;

//	private String IsStartup;
	
//	private String SceneName;
	
//	private String MenuLevel;
	
	private String ColumnName;
	
	private String DayOfWeek;
	
	private long time;
	
//	private String IsNewUser;
	
//	private String IsValidData;
	
//	@Transient
//	private Map<String, String> Extras = new LinkedHashMap<String, String>();
	
//	private String ignores;
	
	public EventItem() {
		super();
	}
	
	

	public void setEventDateTime(Long eventDateTime) {
		Date date = new Date(eventDateTime);
		Date now = new Date();
		if(date.after(now)){
			date = now;
		}
		EventDateTime = TimeUtil.dateTimeFormat.format(date);
		time = date.getTime();
		YearMonth = EventDateTime.substring(0, 7);
		EventDate = EventDateTime.substring(0, 10);
		EventHour = EventDateTime.substring(11, 13);
		DayOfWeek = date.toString().substring(0, 3);
	}

	public String getEventDate() {
		return EventDate;
	}
	
	public String getEventHour() {
		return EventHour;
	}
	
//	public void setExtra(String key, String value) {
//		Extras.put(key, value);
//	}
	

//	public String getIsValidData() {
//		if(IsValidData == null)
//			return "Y";
//		return IsValidData;
//	}
	
	
//	public String getIgnores() {
//		if(Extras!=null){
//			return Extras.toString();
//		}
//		return null;
//	}


	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}


	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
		Map<String, String> ipInfo = IpService.getInstance().findIpInfo(ip);
		this.nation = ipInfo.get("Nation");
		this.province = ipInfo.get("Province");
		this.city = ipInfo.get("City");
		this.operator = ipInfo.get("Operator");
	}


	public String getEvent_screen() {
		return event_screen;
	}



	public void setEvent_screen(String event_screen) {
		this.event_screen = event_screen;
	}



	public String getEvent_action() {
		return event_action;
	}



	public void setEvent_action(String event_action) {
		this.event_action = event_action;
	}



	public String getEvent_reason() {
		return event_reason;
	}



	public void setEvent_reason(String event_reason) {
		this.event_reason = event_reason;
	}



	public String getEvent_label() {
		return event_label;
	}



	public void setEvent_label(String event_label) {
		this.event_label = event_label;
	}



	public String getEvent_value() {
		return event_value;
	}



	public void setEvent_value(String event_value) {
		this.event_value = event_value;
	}



	public String getEvent_extra() {
		return event_extra;
	}



	public void setEvent_extra(String event_extra) {
		this.event_extra = event_extra;
	}



	public void setUser_id(String user_id) {
		this.user_id = user_id;
		if(user_id != null && user_id.matches("^\\d{11}$")){
			this.UserID4Num = user_id;
		}
	}


	public String getSession_id() {
		return session_id;
	}



	public void setSession_id(String session_id) {
		this.session_id = session_id;
	}



	public String getNation() {
		return nation;
	}


	public String getProvince() {
		return province;
	}


	public String getCity() {
		return city;
	}


	public String getOperator() {
		return operator;
	}



	public String getNetwork() {
		return network;
	}


	public void setNetwork(String network) {
		this.network = network;
	}


	public String getSystem_name() {
		return system_name;
	}



	public void setSystem_name(String system_name) {
		this.system_name = system_name;
	}



	public String getSystem_version() {
		return system_version;
	}



	public void setSystem_version(String system_version) {
		this.system_version = system_version;
	}



	public String getClient_device_id() {
		return client_device_id;
	}



	public void setClient_device_id(String client_device_id) {
		this.client_device_id = client_device_id;
	}



	public String getClient_device_brand() {
		return client_device_brand;
	}



	public void setClient_device_brand(String client_device_brand) {
		this.client_device_brand = toUpperCase(client_device_brand);
	}



	public String getClient_device_model() {
		return client_device_model;
	}



	public void setClient_device_model(String client_device_model) {
		this.client_device_model = toUpperCase(client_device_model);
	}



	public String getClient_device_agent() {
		return client_device_agent;
	}



	public void setClient_device_agent(String client_device_agent) {
		this.client_device_agent = client_device_agent;
	}



	public String getClient_device_version() {
		return client_device_version;
	}



	public void setClient_device_version(String client_device_version) {
		this.client_device_version = client_device_version;
	}



	public String getOs_screen() {
		return os_screen;
	}



	public void setOs_screen(String os_screen) {
		this.os_screen = os_screen;
	}



	public String getReferrer() {
		return referrer;
	}



	public void setReferrer(String referrer) {
		this.referrer = referrer;
	}



	public String getCampaign() {
		return campaign;
	}



	public void setCampaign(String campaign) {
		this.campaign = campaign;
	}



	public String getSource() {
		return source;
	}



	public void setSource(String source) {
		this.source = source;
	}



	public String getMedia() {
		return media;
	}



	public void setMedia(String media) {
		this.media = media;
	}



	public String getCreative() {
		return creative;
	}



	public void setCreative(String creative) {
		this.creative = creative;
	}



	public String getRemark() {
		return remark;
	}



	public void setRemark(String remark) {
		this.remark = remark;
	}



	public String getUserID4Num() {
		return UserID4Num;
	}



//	public void setExtras(Map<String, String> extras) {
//		Extras = extras;
//	}


	public String getYearMonth() {
		return YearMonth;
	}

	public String getYearWeek() {
		return YearWeek;
	}

	public void setYearWeek(String yearWeek) {
		YearWeek = yearWeek;
	}


	public String getColumnName() {
		return ColumnName;
	}

	public void setColumnName(String columnName) {
		ColumnName = columnName;
	}

	public String getDayOfWeek() {
		return DayOfWeek;
	}



	private String toUpperCase(String str){
		if(str==null || "".equals(str.trim())){
			return "";
		}
		return str.toUpperCase();
	}
	
	
	private static String decode(String key) {
		try {
			return URLDecoder.decode(key, "utf-8");
		} catch (Exception e) {
			return key;
		}
	}
	

	public static RecordItem convert(String record) {
		RecordItem item = new RecordItem();
		String[] tabs = record.split("&");

		for (String tab : tabs) {
			String[] kv = tab.split("=");

			if (kv.length == 2) {
				String key = decode(kv[0]);
				String value = decode(kv[1]);
				
				try {
					Method method = RecordItem.class.getDeclaredMethod("set" + key, String.class);

					method.invoke(item, value);
				} catch (NoSuchMethodException e) {
					item.setExtra(key, value);
				} catch (Exception e) {
					item.setExtra(key, value);
				}
			}
		}
		return item;
	}
	
	protected String encode(String key) {
		try {
			return URLEncoder.encode(key, "utf-8");
		} catch (Exception e) {
			return key;
		}
	}



	@Override
	public String toString() {
		StringBuffer s =  new StringBuffer();
		s.append("s|event_screen,s|event_action,s|event_reason,s|event_label,s|event_value,s|event_extra,s|user_id,s|session_id,s|EventDateTime,s|ip,s|nation,s|province,s|city,s|operator,s|network,s|system_name,s|system_version,s|client_device_id,s|client_device_brand,s|client_device_model,s|client_device_agent,s|client_device_version,s|os_screen,s|referrer,s|campaign,s|source,s|media,s|creative,s|remark,s|EventDate,s|EventHour,s|UserID4Num,s|YearMonth,s|YearWeek,s|ColumnName,s|DayOfWeek"+COLLECTION_TERMINATED);
		s.append(event_screen+ITEM_TERMINATED+event_action+ITEM_TERMINATED+event_reason+ITEM_TERMINATED+event_label+ITEM_TERMINATED+event_value+ITEM_TERMINATED+event_extra+ITEM_TERMINATED+user_id+ITEM_TERMINATED+session_id+ITEM_TERMINATED+EventDateTime+ITEM_TERMINATED+ip+ITEM_TERMINATED+nation+ITEM_TERMINATED+province+ITEM_TERMINATED+city+ITEM_TERMINATED+operator+ITEM_TERMINATED+network+ITEM_TERMINATED+system_name+ITEM_TERMINATED+system_version+ITEM_TERMINATED+client_device_id+ITEM_TERMINATED+client_device_brand+ITEM_TERMINATED+client_device_model+ITEM_TERMINATED+client_device_agent+ITEM_TERMINATED+client_device_version+ITEM_TERMINATED+os_screen+ITEM_TERMINATED+referrer+ITEM_TERMINATED+campaign+ITEM_TERMINATED+source+ITEM_TERMINATED+media+ITEM_TERMINATED+creative+ITEM_TERMINATED+remark+ITEM_TERMINATED+EventDate+ITEM_TERMINATED+EventHour+ITEM_TERMINATED+UserID4Num+ITEM_TERMINATED+YearMonth+ITEM_TERMINATED+YearWeek+ITEM_TERMINATED+ColumnName+ITEM_TERMINATED+DayOfWeek);
		return time+"\t1\t2\t3\t"+ip+"\t5\t6\t"+ Base64Coder.encodeString(s.toString());
	}


	/*@Override
	public String toString() {
		
		
		String ke = "&";
		StringBuffer record = new StringBuffer();
		
		record.append("IP=" + encode(ip)+ ke);
		record.append("Nation=" + encode(nation)+ ke);
		record.append("Province=" + encode(province)+ ke);
		record.append("City=" + encode(city)+ ke);
		record.append("Operator=" + encode(operator)+ ke);
		record.append("Network=" + encode(network)+ ke);
		record.append("SystemName=" + encode(system_name)+ ke);
		record.append("SystemVersion=" + encode(system_version)+ ke);
		record.append("UserID=" + encode(user_id)+ ke);
		record.append("SessionID=" + encode(session_id)+ ke);
		record.append("ClientDeviceID=" + encode(client_device_id)+ ke);
		record.append("ClientDeviceBrand=" + encode(client_device_brand)+ ke);
		record.append("ClientDeviceModel=" + encode(client_device_model)+ ke);
		record.append("ClientDeviceAgent=" + encode(client_device_agent)+ ke);
		record.append("ClientDeviceVersion=" + encode(client_device_version)+ ke);
		record.append("OsScreen=" + encode(os_screen)+ ke);
		record.append("EventDateTime=" + encode(EventDateTime)+ ke);
		record.append("EventDate=" + encode(EventDate)+ ke);
		record.append("EventHour=" + encode(EventDate)+ ke);
		record.append("EventScreen=" + encode(event_screen)+ ke);
		record.append("EventAction=" + encode(event_action)+ ke);
		record.append("EventLabel=" + encode(event_label)+ ke);
		record.append("EventValue=" + encode(event_value)+ ke);
		record.append("Referrer=" + encode(referrer)+ ke);
		record.append("Campaign=" + encode(campaign)+ke);
		record.append("Source=" + encode(source)+ ke);
		record.append("Media=" + encode(media)+ ke);
		record.append("Creative=" + encode(creative)+ ke);
		
		record.append("Content=" + encode(event_reason)+ ke);
		record.append("UserID4Num=" + encode(UserID4Num)+ ke);
		record.append("OptName=" + encode(OptName)+ ke);
		record.append("YearMonth=" + encode(YearMonth)+ ke);
		record.append("YearWeek=" + encode(YearWeek)+ ke);
		record.append("SceneName=" + encode(SceneName) + ke);
		record.append("MenuLevel=" + encode(MenuLevel) + ke);
		record.append("ColumnName=" + encode(event_extra) + ke);
		record.append("DayOfWeek=" + encode(DayOfWeek) );
		
		return record.toString();
	}*/

	
	
}
